#!/usr/bin/env python
"""
Nagios plugin that monitors the core voms services. 
Notice, the pid location paths are hardcoded. 
"""
# last change: adaptation for EMI Kebnekaise directory structure

__author__="Placi Flury grid@switch.ch"
__copyright__ = "Copyright 2010, SMSCG an AAA/SWITCH project"
__date__="27.08.2009"
__version__="0.2.0"

import sys
import commands
from nagios_plugin import NagiosPluginBasic,LocalService
from nagios_plugin import RETURN_CODE


MYSQL_PID_LOCATION = "/var/run/mysqld"
TOMCAT_PID_LOCATION = "/var/run"
VOMS_PID_LOCATION="/opt/glite/var/lock/subsys"
VOS = ["smscg","switch","crypto","life","earth"]

class VOMSCore(NagiosPluginBasic):
    """
    Checks whether GridFtp service is running. It's a local check
    without any connectivity testing.
    """
    def __init__(self):
        parser = NagiosPluginBasic.getOptions(self)
        parser.remove_option("-c")   # removing thresholds
        parser.remove_option("-w")
        parser.remove_option("-H")   # remove hostname option
        parser.parse_args()


    def main(self):
        service = LocalService("mysqld",MYSQL_PID_LOCATION)
        msg, return_code = service.getstate()
        if return_code != RETURN_CODE["ok"]:
            print msg
            sys.exit(return_code)

        service = LocalService("httpd")
        msg, return_code = service.getstate()
        if return_code != RETURN_CODE["ok"]:
            print msg
            sys.exit(return_code)

        service = LocalService("tomcat5",TOMCAT_PID_LOCATION)
        msg, return_code = service.getstate()
        if return_code != RETURN_CODE["ok"]:
            print msg
            sys.exit(return_code)

        # vo instances checks 
        for vo in VOS:
            service = LocalService(name="vo")

            pid = -1
            # taken from voms init.d script
            cmd = "ps axo pid,args | grep 'conf /etc/voms/%s/voms.conf' | grep -v grep | cut -f 1 -d '/'|head -1" % (vo)
            pid = commands.getoutput(cmd) 

            try:
                pid = int(pid)
            except:
                print "UNKNOWN: while checking VO %s" % vo
                sys.exit(RETURN_CODE["unknown"])
            if pid == -1:
                print "CRITICAL: VO %s is not running" % vo
                sys.exit( RETURN_CODE["critical"])

            state = service.getprocstate(pid)

            if not state:
                print "UNKNOWN: while checking VO  %s." % vo
                sys.exit(RETURN_CODE["unknown"])
            if state == 'A':
                print "WARNING: access to proc filesystem denied (insufficient permissions)."
                sys.exit(RETURN_CODE["warning"])
            if state in ['X','Z']:
                print "CRITICAL: VO process %s in state ('%s')." % (vo,state)
                sys.exit( RETURN_CODE["critical"])
            if state in ['T','W']:
                print "WARNING: %s in state ('%s')." % (vo,state)
                sys.exit(RETURN_CODE["warning"])

        print "OK: Core VOMS and VO(s) %s are up and running." % str(VOS)
        sys.exit(RETURN_CODE["ok"])


if __name__ =="__main__":
    g = VOMSCore()
    g.main()
